<template>
  <div>
    <h2>添加员工</h2>账号
    <input type="text" v-model="username" />
    密码
    <input type="text" v-model="password" />
    部门
    <select v-model="depart">
      <option v-for="i in departs" :key="i.id" :value="i.id">{{i.name}}</option>
    </select>
    <button @click="sub">添加部门</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      username: "",
      password: "",
      departs: [],
      depart: 0
    };
  },
  mounted() {
    this.axios({
      url: "http://127.0.0.1:8000/app01/department/",
      method: "get"
    }).then(res => {
      this.departs = res.data.data;
    });
  },
  methods: {
    sub: function() {
      let auth = localStorage.getItem("auth");
      let data = {
        username: this.username,
        password: this.password,
        depart: this.depart,
        auth: auth
      };

      this.axios({
        url: "http://127.0.0.1:8000/app01/user/",
        method: "post",
        data: data
      }).then(res => {
        if (res.data.code == 1000) {
          //
          this.$router.push("/user");
        } else {
          alert(res.data.msg);
        }
      });
    }
  }
};
</script>

<style>
</style>